<?php
$storage = Zend_Auth::getInstance()->getStorage();
$xml = 'public/xml/menu_'.$storage->read()->USU_ID.'.xml';
if(is_file($xml))
{
    $reader = new Zend_Config_Xml($xml);
    $menus = $reader->toArray();
//echo '<pre>';print_r($menus);echo '</pre>';
    
// seleciona menu para ficar visivel
$menuShow = '';
foreach ($menus as $menu){ 
    foreach ($menu as $men){
        if(isset($men['nome'])) $men = array('0' => $men);   
        if(is_array($men))
        foreach ($men as $m){
            foreach ($m['modulos'] as $mod){ 
                if (isset($mod[0])){
                    foreach ($mod as $item)
                        if($this->controller == $item['arquivo']) 
                            $menuShow = $m['nome'];
                }else{
                    if($this->controller == $mod['arquivo']) 
                        $menuShow = $m['nome'];
                }
            }        
        }
    }
}
?>

<ul class="nav navbar-nav">
    <?php /*
    <li>
        <a class="m_i_home" href="<?php echo $this->baseUrl($this->modulo.'/'); ?>">Início</a>
    </li> */ ?>
<?php
foreach ($menus as $menu) {
    foreach ($menu as $men) {
        // corrigir erro quando tem apenas um menu.
        if(isset($men['nome'])) $men = array('0' => $men);
        if(is_array($men))
        foreach ($men as $m) {            
            
            $style = ($menuShow == $m['nome']) ? 'active':'';
            $class = ($menuShow == $m['nome']) ? 'class="active"':'';
            
?>
    
    <li class="dropdown <?php echo $style?>" >
        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
            <?php echo $m['nome']; ?> <span class="caret"></span>
        </a>
        
        <ul class="dropdown-menu" role="menu">
            <?php
            foreach ($m['modulos'] as $mod) {
                if(isset($mod[0])) {
                    foreach ($mod as $item) { 
                        $class = ($this->controller == $item['arquivo']) ? 'class="active"':'';
                    ?>  
                    <li><a <?php echo $class; ?> href="<?php echo $this->baseUrl($this->modulo.'/'.$item['arquivo']); ?>"><?php echo $item['nome']; ?></a></li>
                <?php
                    }
                } else if (isset($mod['id'])) { 
                    $class = ($this->controller == $item['arquivo']) ? 'class="active"':'';
                ?>  
                    <li><a <?php echo $class; ?> href="<?php echo $this->baseUrl($this->modulo.'/'.$mod['arquivo']); ?>"><?php echo $mod['nome']; ?></a></li>
                <?php
            }
        }
        $img = glob('public/upload/menu/'.$m['id'].'.*');
        if(isset($img[0])){ 
        ?>
        <div class="fd-img">  
            <img src="<?php echo $this->baseUrl('/').$img[0]; ?>" />
        </div>
        <?php } ?>
        </ul>
        
    </li>
    
<?php       
        }
    }
}
/* onclick="if(confirm('Deseja sair do sistema?')) location = '<?php echo $this->baseUrl($this->modulo.'/auth/logout')?>';" */
?>
    <li>
        <a class="m_i_exit" href="javascript:void(0)">Sair</a>
    </li>
</ul>


<?php
}
else echo '<br>Arquivo de menu inexistente!<br>';

?>
